#define NUM_MAX 1000

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char** findRestaurant(char** list1, int list1Size, char** list2, int list2Size, int* returnSize)
{
    int i, j;
    int min_index_sum = INT_MAX;
    int return_size = 0;
    for (i = 0; i < list1Size; i++)
    {
        for (j = 0; j < list2Size; j++)
        {
            if (strcmp(list1[i], list2[j]) == 0 && (i + j < min_index_sum))
            {
                min_index_sum = i + j;
            }
        }
    }
    char** p = (char**)malloc(NUM_MAX * sizeof(char*));
    for (i = 0; i < list1Size; i++)
    {
        for (j = 0; j < list2Size; j++)
        {
            if (strcmp(list1[i], list2[j]) == 0 && (i + j == min_index_sum))
            {
                p[return_size] = (char*)malloc(strlen(list1[i]) + 1);
                if (p[return_size] == NULL)
                {
                    return NULL;
                }
                strcpy(p[return_size], list1[i]);
                return_size++;
            }
        }
    }
    *returnSize = return_size;
    return p;
}
